# _*_ coding: utf-8 _*_
__author__ = 'yinlei'
__date__ = '2018-05-18 8:22'

import enum

import shortuuid
from werkzeug.security import generate_password_hash, check_password_hash

from apps.bases.base_mode import BaseMode
from exts import db
from utils import digital


class GenderEnum(enum.Enum):
    MALE = 1
    FEMALE = 2
    SECRET = 3
    UNKNOW = 4


class FrontUser(BaseMode):
    __tablename__ = 'front_user'
    id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid)
    mobile = db.Column(db.String(11), nullable=False, unique=True)
    username = db.Column(db.String(50), nullable=False, default=digital.get_random_digital)
    _password = db.Column('password', db.String(300), nullable=False)
    email = db.Column(db.String(50), unique=True)
    real_name = db.Column(db.String(50))
    avatar = db.Column(db.String(100))
    signature = db.Column(db.String(100))
    gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOW)
    posts = db.relationship('PostModel',back_populates='author')
    comments = db.relationship('CommentModel',back_populates='author')

    def __init__(self, *args, **kwargs):
        if 'password' in kwargs:
            self.password = kwargs.get('password')
            kwargs.pop('password')
        super(FrontUser, self).__init__(*args, **kwargs)

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, newpwd):
        self._password = generate_password_hash(newpwd)

    def check_password(self, rawpwd):
        return check_password_hash(self._password, rawpwd)
